package com.cdj.common.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.cdj.common.ILoadingBehavior;
import com.cdj.common.behavior.DefualtLoadingBehavior;

/**
 * ---------------------------------------------------------<br />
 * desc：<br />
 * author：chendeji <br />
 * date：2021/6/26<br />
 * email：18701434169@163.com<br />
 * qq: 781571323
 * wx: melody_2009
 * ---------------------------------------------------------<br />
 */
public abstract class BaseLoadDataFragment extends BaseFragment{

    protected ILoadingBehavior defualLoadingBehavior;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setDefualLoadingBehavior(new DefualtLoadingBehavior(getActivity()));
    }

    public void setDefualLoadingBehavior(ILoadingBehavior defualLoadingBehavior) {
        this.defualLoadingBehavior = defualLoadingBehavior;
    }

    protected ILoadingBehavior getLoadingBehavior() {
        return defualLoadingBehavior;
    }

    /**
     * desc：显示加载提示内容<br />
     * author：陈德基<br />
     * date：2021/6/3<br />
     * qq: 781571323
     * wx: melody_2009
     */
    protected void showLoading() {
        if (defualLoadingBehavior != null) {
            defualLoadingBehavior.onLoading();
        } else {
            showActivityLoading();
        }
    }

    private void showActivityLoading() {
        if (getActivity() instanceof ILoadingBehavior) {
            ILoadingBehavior loadingBehavior = (ILoadingBehavior) getActivity();
            loadingBehavior.onLoading();
        }
    }

    private void finishActivityLoading() {
        if (getActivity() instanceof ILoadingBehavior) {
            ILoadingBehavior loadingBehavior = (ILoadingBehavior) getActivity();
            loadingBehavior.finishLoad();
        }
    }

    /**
     * desc：结束加载<br />
     * author：陈德基<br />
     * date：2021/6/3<br />
     * qq: 781571323
     * wx: melody_2009
     */
    protected void finishLoading() {
        if (defualLoadingBehavior != null) {
            defualLoadingBehavior.finishLoad();
        } else {
            finishActivityLoading();
        }
    }

}
